package reflectdemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {

    public static void reflectNewInstance1(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("reflectdemo.Student");
            Student student = (Student) c1.newInstance();

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectNewInstance(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("reflectdemo.Student");
            Constructor<Student> constructor =
                    (Constructor<Student>)c1.getDeclaredConstructor(String.class,int.class);

            //先置为true，才能调用 private Student 这一构造方法
            constructor.setAccessible(true);
            Student student = constructor.newInstance("linxi",18);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateConstructor() {
        Class<?> c1 = null;
        try {
            c1 = Class.forName("reflectdemo.Student");
            Constructor<Student> constructor =
                    (Constructor<Student>)c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);

            Student student = constructor.newInstance("linxi",18);

            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateField(){
        //同样，首先必须有一个class对象
        Class<?> c1 = null;
        try {
            c1 = Class.forName("reflectdemo.Student");

            Constructor<Student> constructor =
                    (Constructor<Student>)c1.getDeclaredConstructor();
            Student student = constructor.newInstance();

            Field field = c1.getDeclaredField("name");

            //由于此字段是私有的，所以我们应该先调用setAccessible并将其置为true
            field.setAccessible(true);

            field.set(student,"wangmo");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    public static void reflectPrivateMethod(){
        Class<?> c1 = null;
        try {
            c1 = Class.forName("reflectdemo.Student");
            Constructor<Student> constructor =
                    (Constructor<Student>)c1.getDeclaredConstructor();
            Student student = constructor.newInstance();

            Method method = c1.getDeclaredMethod("function",String.class);
            method.setAccessible(true);

            method.invoke(student,"我尝试修改一下function方法的参数");


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        reflectNewInstance();
//        reflectPrivateField();
        reflectPrivateMethod();
    }
}
